#!/usr/bin/env python
import os, sys
import pygame
from pygame.locals import *

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

TIME=250
CAMP="Grafics/ground_1.png"
EQUIP1="Grafics/Equip1.png"
EQUIP2="Grafics/Equip2.png"
PILOTA="Grafics/Pilota.png"

def load_image(name, colorkey=None):
    fullname = os.path.join('soccerbots/simple_gui/data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

class SimpleGUI:
	def __init__(self):
		self.position = {0:[] , 1:[]}
		self.rotation = {0:[] , 1:[]}
		self.ball = [0,0]
		pygame.init()
		self.screen = pygame.display.set_mode((800,577))
		pygame.display.set_caption('Visual Server')
		self.background, self.backgroundRect = load_image(CAMP)
		self.images=[load_image(EQUIP1,-1)]
		self.images.append(load_image(EQUIP2,-1))
		self.images.append(load_image(PILOTA,-1))
		self.screen.blit(self.background,self.backgroundRect)
		self.definition_real_size(2500, 1800)

	'''Redibuixar tots els jugadors'''
	def draw(self):
		for event in pygame.event.get():
            		if event.type == QUIT:
                		return
	            	elif event.type == MOUSEBUTTONDOWN:
        	        	fist.punch()
	        	elif event.type is MOUSEBUTTONUP:
		        	fist.unpunch()

		self.screen.blit(self.background,self.backgroundRect)
		for i in range(len(self.position[0])):
			for team in [0,1]:
				img = self.__prepare_player(team,i)
				self.screen.blit(img,(self.position[team][i][0]-img.get_width()/2,self.position[team][i][1]-img.get_height()/2))
			img = self.__prepare_ball()
			self.screen.blit(img,(self.ball[0]-img.get_width()/2, self.ball[1]-img.get_height()/2))		
		pygame.display.flip()
		self.__wait()


	'''Registrem un nou jugador 
		new_player(EQUIP,POSICIONS(X,Y),ANGLE)'''
	def new_player(self,team,x,y,rot):
		self.position[team].append(self.__conversion(x,y))
		self.rotation[team].append(rot)


	'''Refrescar el moviment dels jugadors'''
	def player_movement(self,team,player,x,y,rotation):
		self.position[team][player]=self.__conversion(x,y)
		self.rotation[team][player]=rotation


	def ball_position(self,x,y):
		pos=self.__conversion(x,y)
		self.ball[0]=pos[0]
		self.ball[1]=pos[1]


	def definition_real_size(self,x,y):
		self.real_weight=x
		self.real_height=y


	def __prepare_ball(self):
		return self.images[2][0]


	'''PRIVATE Function'''
	def __prepare_player(self,team,player):
		if self.rotation[team][player] != 0:
			img=pygame.transform.rotate(self.images[team][0],self.rotation[team][player])
			return img
		return self.images[team][0]


	def __wait(self):
		pygame.time.delay(TIME)


	def __conversion(self,x,y):
	 	x /= 100.
		y /= 100.
		return ( 400 + x * 100 * 800. / self.real_weight, 577 - y * 100 * 577. / self.real_height)


'''if __name__ == "__main__":
	teto = Teto()
	teto.new_player(0,300,200,0.0)
	teto.new_player(1,400,400,45.0)
	x=0.0
	while True:
		teto.draw()
		x=x+10.0
		teto.player_movement(0,0,300,200,x)'''
